package com.xuegao.xuegaololskills.JXGrabKey;

import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import jxgrabkey.HotkeyConflictException;
import jxgrabkey.HotkeyListener;
import jxgrabkey.JXGrabKey;

public class JXGrabKeyTest {

    private static final int MY_HOTKEY_INDEX_0 = 0;
    private static final int MY_HOTKEY_INDEX_1 = 1;
    private static final int MY_HOTKEY_INDEX_2 = 2;
    private static final int MY_HOTKEY_INDEX_3 = 3;
    private static final int MY_HOTKEY_INDEX_4 = 4;
    private static final int MY_HOTKEY_INDEX_5 = 5;
    private static final int MY_HOTKEY_INDEX_6 = 6;
    private static final int MY_HOTKEY_INDEX_7 = 7;
    private static final int MY_HOTKEY_INDEX_8 = 8;
    private static final int MY_HOTKEY_INDEX_9 = 9;
    private static final int MY_HOTKEY_INDEX_100 = 100;
    private static boolean hotkeyEventReceived = false;

    static List<Character> singleInput = new ArrayList<Character>();

    private static String caseCode() {
        StringBuffer buffer = new StringBuffer();
        for (Character i : singleInput) {
            buffer.append(i);
        }
        return buffer.toString();
    }

    public static void main(String[] args) throws Exception {
        //Load JXGrabKey lib
        System.load(new File("libs/libJXGrabKey.so").getCanonicalPath());

        //Enable Debug Output
        JXGrabKey.setDebugOutput(true);

        //Register some Hotkey
        try {
            //int key = KeyEvent.VK_K, mask = KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK;
            //int key = KeyEvent.VK_F2, mask = KeyEvent.ALT_MASK; //Conflicts on GNOME

            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_0, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_0);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_1, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_1);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_2, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_2);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_3, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_3);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_4, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_4);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_5, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_5);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_6, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_6);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_7, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_7);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_8, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_8);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_9, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_9);
            JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_100, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_ENTER);
        } catch (HotkeyConflictException e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE);

            JXGrabKey.getInstance().cleanUp(); //Automatically unregisters Hotkeys and Listeners
            //Alternatively, just unregister the key causing this or leave it as it is 
            //the key may not be grabbed at all or may not respond when numlock, capslock or scrollock is on
            return;
        }

        //Implement HotkeyListener
        HotkeyListener hotkeyListener = new jxgrabkey.HotkeyListener() {
            public void onHotkey(int hotkey_idx) {
                switch (hotkey_idx) {
                    case MY_HOTKEY_INDEX_0:
                        singleInput.add((char) 0);
                    case MY_HOTKEY_INDEX_1:
                        singleInput.add((char) 1);
                    case MY_HOTKEY_INDEX_2:
                        singleInput.add((char) 2);
                    case MY_HOTKEY_INDEX_3:
                        singleInput.add((char) 3);
                    case MY_HOTKEY_INDEX_4:
                        singleInput.add((char) 4);
                    case MY_HOTKEY_INDEX_5:
                        singleInput.add((char) 5);
                    case MY_HOTKEY_INDEX_6:
                        singleInput.add((char) 6);
                    case MY_HOTKEY_INDEX_7:
                        singleInput.add((char) 7);
                    case MY_HOTKEY_INDEX_8:
                        singleInput.add((char) 8);
                    case MY_HOTKEY_INDEX_9:
                        singleInput.add((char) 9);
                    case MY_HOTKEY_INDEX_100:
                        System.out.println(caseCode());
                        singleInput.clear();
                }
                //hotkeyEventReceived = true;
            }
        };

        //Add HotkeyListener
        JXGrabKey.getInstance().addHotkeyListener(hotkeyListener);

//        //Wait for Hotkey Event
//        while(!hotkeyEventReceived){
//            Thread.sleep(1000);
//        }
//        
//        // Shutdown JXGrabKey
//        JXGrabKey.getInstance().unregisterHotKey(MY_HOTKEY_INDEX); //Optional
//        JXGrabKey.getInstance().removeHotkeyListener(hotkeyListener); //Optional
//        JXGrabKey.getInstance().cleanUp();
    }
}